package com.fyzk.activity.user.accountdetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseFragment;
import com.fyzk.activity.user.NetProgressView;
import com.fyzk.bean.WithdrawInfo;
import com.fyzk.interfaces.MoreInterface;
import com.fyzk.interfaces.ReTryInterface;
import com.fyzk.interfaces.UpdateInterface;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.MsgListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by miao on 2017/5/20.
 */
public class WithDrawRecordFragment extends BaseFragment implements MoreInterface,
        UpdateInterface, ReTryInterface {

    int pageIndex = 1;

    private MsgListView recordlist;

    private NetProgressView recordProgressView;

    int timeRange = 2;


    /**
     * pageIndex	int	页码
     * maxResult	int	每页记录数
     * timeRange	int	时间范围
     */
    protected void request_accountDetail() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("pageIndex", pageIndex);
            request_json.put("maxResult", 20);
            request_json.put("timeRange", timeRange);
            httpPostJson(CommandUtil.FINDUSERDRAWAMOUNTS, Commands.FINDUSERDRAWAMOUNTS,
                    request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * pageIndex	int	页码
     * maxResult	int	每页记录数
     * timeRange	int	时间范围
     */
    public void request_accountDetailTimeRange(int timeRange) {
        recordProgressView.waiting();
        this.timeRange = timeRange;
        pageIndex = 1;
        request_accountDetail();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fg_withdrawrecord, null);
    }


    ArrayList<WithdrawInfo> withdrawInfos = new ArrayList<>();

    /**
     * 为记录列表增加
     *
     * @param infos
     */
    public void addRecordInfos(List<WithdrawInfo> infos) {
        if (null != infos) {
            this.withdrawInfos.addAll(infos);
        }
        if (infos.size() < 20) {
            recordlist.listMore.setClickable(false);
            recordlist.listMore.setTextStr("无更多数据");
        } else {
            recordlist.listMore.endNet();
        }
        listAdapter.notifyDataSetChanged();
    }


    /**
     * 初始化记录列表
     *
     * @param infos
     */
    public void initRecordList(List<WithdrawInfo> infos) {
        if (infos.size() > 0) {
            if (null != infos) {
                this.withdrawInfos.clear();
                this.withdrawInfos.addAll(infos);
            }
            if (infos.size() < 20) {
                recordlist.listMore.setClickable(false);
                recordlist.listMore.setTextStr("无更多数据");
            } else {
                recordlist.listMore.endNet();
            }
            listAdapter.notifyDataSetChanged();
        }
    }

    WithdrawListAdapter listAdapter;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recordlist = (MsgListView) view.findViewById(R.id.recordlist);
        recordProgressView = (NetProgressView) view.findViewById(R.id.recordProgressView);
        recordlist.setMoreInterface(this);
        recordlist.setUpdateInterface(this);
        recordlist.setItemsCanFocus(false);
        recordlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listAdapter = new WithdrawListAdapter(getContext(), withdrawInfos);
        recordlist.setAdapter(listAdapter);
        recordProgressView.setRetryInterface(this);
        request_accountDetail();
    }

    @Override
    public void more() {
        pageIndex++;
        request_accountDetail();
    }


    @Override
    public void onLeftClick() {
        super.onLeftClick();
    }


    @Override
    public void update() {
        pageIndex = 1;
        request_accountDetail();
    }

    @Override
    public void reTry() {
        recordProgressView.waiting();
        pageIndex = 1;
        request_accountDetail();
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        recordProgressView.dismiss();
        try {
            JSONObject jsonObject = new JSONObject(resultBean.getData());
            String details = jsonObject.getString("details");
            List<WithdrawInfo> withdrawInfos = JSON.parseArray(details, WithdrawInfo.class);
            if (pageIndex == 1) {
                initRecordList(withdrawInfos);
            } else {
                addRecordInfos(withdrawInfos);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            recordlist.onRefreshComplete();
        }

    }

    @Override
    public void onFailure(int requestCode, String errorcode, String msg) {
        if (errorcode.equals("900003")) {
            recordProgressView.noneRecord(msg);
        } else {
            showToast(msg);
        }
    }
}
